package com.tin.service;

import com.tin.bean.TokenInfo;
import com.tin.bean.User;
import com.tin.dao.TokenDao;
import com.tin.dao.UserDao;
import com.tin.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;


@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private TokenDao tokenDao;


    public User findUserByUsernameameAndPassword(String username, String password) {
        return userDao.findByUsernameAndPassword(username, password);
    }

    public List<TokenInfo> findTokenByUsername(String username) {
        return tokenDao.findByUsername(username);
    }

    public void deleteTokenByUsername(String username) {
        tokenDao.deleteByUsername(username);
    }

    public Integer addUser(User user) {
        return userDao.insert(user.getUsername(), user.getPassword(), user.getMail(), user.getCreateDate(), user.getRole());
    }

    public Integer addToken(TokenInfo tokenInfo) {
        return tokenDao.insertTokenInfo(tokenInfo.getUsername(),tokenInfo.getToken(),tokenInfo.getIp(),tokenInfo.getExpireDate());
    }

    public User findUserByUsername(String username) {
        return userDao.findByUsername(username);
    }

    public void deleteUser(List<User> list) {
        for (User user:list) {
            userDao.delete(user);
        }
    }

    public void deleteUser(User user){
        userDao.delete(user);
    }

    public void deleteByToken(String token) {
        tokenDao.deleteByToken(token);
    }

    public void updateExpireDateByToken(String expireDate, String token) {
        tokenDao.updateExpireDateByToken(expireDate, token);
    }
}
